// ----------------------------------
// RSDK Project: Sonic The Essence Hog & Edgy Duh Hedgy Gunless
// Script Description: Text Cutscene Object
// Script Author: KarlEmerald
// ----------------------------------

// Aliases from Main Object
private alias object.value2 : object.currentLine
private alias object.value3 : object.NumberOfLetters
private alias object.value4 : object.currentColor

// Aliases
private alias object.value0 : object.DisableDraw
private alias object.value1 : object.textOffset
private alias object.value2 : object.textHelper
private alias object.value3 : object.textHelperWidth 
private alias object.value4 : object.textHelperFinder
private alias object.value5 : object.rowCounter
private alias object.value6 : object.currentWord
private alias object.value7 : object.textColor
private alias object.value8 : object.linelength
private alias object.value9 : object.rowStopper // prevents anything bad from happening regarding rows like an extra row
private alias object.value10 : object.originalY
private alias object.value11 : object.originalX
private alias object.value12 : object.MainObjEntityPos


private alias object.value37 : object.red
private alias object.value38 : object.blu
private alias object.value39 : object.grn
private alias object.value40 : object.store
private alias object.value41 : object.process
// Property Value
private alias 0 : TEXT_TEXTHANDLER

// Other
private alias 12 : TEXT_YSPACING
private alias 0 : CHAR_OFFSET

// Function declarations

// Static Values
private value TextBoxXBoundary = 0;

// Tables
private table ColorTable
	0xE0E0E0
end table

public table UpperCaseOption
	7, 7, 7, 7, 7, 7, 7, 7, 3, 7, 7, 7, 9, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9, 7, 7, 7
end table

public table LowerCaseOption
	7, 7, 7, 7, 7, 7, 7, 7, 3, 7, 7, 3, 9, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9, 7, 7, 7
end table

event ObjectMain
	
end event

event ObjectDraw
	if object.propertyValue == TEXT_TEXTHANDLER
		// Set offsets
		temp1 = CHAR_OFFSET
		object.currentWord = 1

		// Positions
		temp4 = object.ixpos
		temp3 = object.iypos

		// I mean... sure karl
		temp2 = 0	

		// Row Related
		object.rowCounter = 0
		TextBoxXBoundary = screen.xcenter
		TextBoxXBoundary += 125
		object.textHelperWidth = 0

		// Get line length and start the loop.
		GetTextInfo(object.linelength, 22, TEXTINFO_TEXTSIZE, object[-1].currentLine, 0)
		while temp1 < object.linelength

			// grab the charater and assign the frame
			GetTextInfo(temp2, 22, TEXTINFO_TEXTDATA, object[-1].currentLine, temp1)
			switch temp2 
			default
				temp7 = temp2
				if temp7 > 96 //If it's lowercase
					temp7 -= 97
					temp7 += 28
				else
					temp7 -= 65
					temp7 += 2
				end if
				
				object.frame = temp7
				break
			case 91
				//someone wants to do something so we can't draw
				object.DisableDraw = 1
				break
			case 32
				object.frame = 71
				break
			case 40
				object.frame = 64
				break				
			case 41	
				object.frame = 65
				break				
			case 34	
				object.frame = 66
				break	
			case 46
				object.frame = 67
				break					
			case 63
				object.frame = 72
				break					
			case 45
				object.frame = 68
				break				
			case 49	
				object.frame = 55
				break
			case 48	
				object.frame = 54
				break
			case 44	
				object.frame = 69
				break				
			case 33	
				object.frame = 70
				break				
			case 39
				object.frame = 73
				break							
			case 58
				object.frame = 74
				break				
			case 50
			case 51
			case 52
			case 53
			case 54
			case 55
			case 56
			case 57
				temp7 = temp2
				temp7 += 6
				object.frame = temp7
				break				
			end switch

			// If we're not doing any specific text commands
			if object.DisableDraw == 0

				//Create the text.
				arrayPos0 = 600
				arrayPos0 += temp1
				ResetObjectEntity(arrayPos0, TypeName[Text], object.currentWord, 0, 0)
				object[arrayPos0].ixpos = temp4
				object[arrayPos0].iypos = temp3
				object[arrayPos0].frame = object.frame
				object[arrayPos0].textHelperFinder = temp2
				object[arrayPos0].drawOrder = 6
				object[arrayPos0].priority = PRIORITY_ACTIVE
				object[arrayPos0].MainObjEntityPos = object[-1].entityPos

				// Spacing
				switch temp2
				case 32	// When using space, move onto the next word
					object.currentWord++
					temp4 += 6					
					break
				case 59 // force a row
					temp3 += TEXT_YSPACING
					temp4 = 10
					if object.rowStopper == 0
						object.rowCounter++
						object.rowStopper = 1
					end if					
					break
				case 40			
				case 41								
				case 49							
				case 34			
					temp4 += 5
					break	
				case 33
				case 46
				case 39
				case 58
					temp4 += 3
					break					
				case 45	
					temp4 += 6
					break				
				case 48	
				case 63	
					temp4 += 7
					break
				case 44		
					temp4 += 4
					break									
				default
					if temp2 >= 97
						if temp2 <= 122 
							temp7 = temp2 
							temp7 -= 97
							GetTableValue(temp6, temp7, LowerCaseOption)	
						end if
					else
						if temp2 >= 65
							if temp2 <= 90 				
								temp7 = temp2 
								temp7 -= 65	
								GetTableValue(temp6, temp7, UpperCaseOption)
							end if
						else
							temp6 = 7	
						end if		
					end if					
					temp4 += temp6
					break
				end switch
				temp4--

				// Check if letter position surpasses text box limit
				if temp4 >= TextBoxXBoundary					
					foreach(TypeName[Text], arrayPos0, ALL_ENTITIES)
						if object[arrayPos0].propertyValue == object.currentWord // if this is the word that exceeds the boundary		

							// do your thing row counter
							if object.rowStopper == 0
								object.rowCounter++
								object.rowStopper = 1
							end if			

							// Set position for new row
							temp3 = object.iypos	

							temp6 = TEXT_YSPACING
							temp6 *= object.rowCounter	

							temp3 += temp6	

							temp7 = object.originalY
							temp7 += temp6
							temp7 -= TEXT_YSPACING								

							arrayPos1 = 0							
							while arrayPos1 < 0x420
								if object[arrayPos1].type == TypeName[Text]
									if object[arrayPos1].propertyValue == object.currentWord
										if object[arrayPos1].iypos == temp7
											temp2 = object[arrayPos1].textHelperFinder

											object[arrayPos1].ixpos = object.originalX																																									
											object[arrayPos1].ixpos += object.textHelperWidth	
											if temp2 != 32											
												switch temp2
												case 40			
												case 41								
												case 49							
												case 34			
													object.textHelperWidth += 5
													break	
												case 33
												case 46
												case 39
												case 58
													object.textHelperWidth += 3
													break					
												case 45	
													object.textHelperWidth += 6
													break				
												case 48	
												case 63	
													object.textHelperWidth += 7
													break
												case 44		
													object.textHelperWidth += 4
													break					
												default
													if temp2 >= 97
														if temp2 <= 122 
															temp7 = temp2 
															temp7 -= 97
															GetTableValue(temp6, temp7, LowerCaseOption)	
														end if
													else
														if temp2 >= 65
															if temp2 <= 90 				
																temp7 = temp2 
																temp7 -= 65	
																GetTableValue(temp6, temp7, UpperCaseOption)
															end if
														else
															temp6 = 7	
														end if		
													end if					
													object.textHelperWidth += temp6
													break
												end switch
												
											end if
											object[arrayPos1].iypos += TEXT_YSPACING																																																														
										end if 
									end if
								end if							
								arrayPos1++							
							loop	
							temp4 = object.originalX	
							temp4 += object.textHelperWidth	
						end if	
					next
				end if	
			else
				// Color Change: It's here since all the drawing is done at once.
				temp5 = temp1
				temp5++
				GetTextInfo(temp2, 22, TEXTINFO_TEXTDATA, object[-1].currentLine, temp5)
				if temp2 == 99 // if c
					temp5++
					GetTextInfo(temp6, 22, TEXTINFO_TEXTDATA, object[-1].currentLine, temp5)
					temp0 = temp6
					if temp6 >= 65
						temp0 -= 55
					else
						if temp6 >= 48
							temp0 -= 48
						end if	
					end if	
					// set it to a specific number and assign it.
					object[-1].currentColor = temp0
				end if	
			end if			

			//reset these two	
			object.rowStopper = 0
			object.textHelperWidth = 0

			// nice go on now
			temp1++

			// If we're done using any sort of text command, resume the drawing.
			GetTextInfo(temp2, 22, TEXTINFO_TEXTDATA, object[-1].currentLine, temp1)			
			if temp2 == 93
				object.DisableDraw = 0
				temp1++
			end if				
		loop
	else
		// Give the text a color

		// Draw, but make sure it's actually time to draw.
		temp0 = object.entityPos
		temp0 -= 600

		arrayPos0 = object.MainObjEntityPos

		if object[arrayPos0].NumberOfLetters >= temp0
			SetPaletteEntry(0, 6, 0000)
			temp1 = object.ixpos
			temp1--
			DrawSpriteScreenXY(object.frame, temp1, object.iypos)
			temp1 += 2
			DrawSpriteScreenXY(object.frame, temp1, object.iypos)

			temp2 = object.iypos
			temp2++
			temp1 = object.ixpos
			temp1--
			DrawSpriteScreenXY(object.frame, temp1, temp2)
			temp1 += 2
			DrawSpriteScreenXY(object.frame, temp1, temp2)

			temp2 = object.iypos
			temp2--
			temp1 = object.ixpos
			temp1--
			DrawSpriteScreenXY(object.frame, temp1, temp2)
			temp1 += 2
			DrawSpriteScreenXY(object.frame, temp1, temp2)

			temp2 = object.iypos
			temp2--
			DrawSpriteScreenXY(object.frame, object.ixpos, temp2)
			DrawSpriteScreenXY(object.frame, object.ixpos, temp2)

			temp2 = object.iypos
			temp2++
			DrawSpriteScreenXY(object.frame, object.ixpos, temp2)
			DrawSpriteScreenXY(object.frame, object.ixpos, temp2)

			temp1 = color7
			temp1 <<= 16				
			
			temp2 = color7
			temp2 <<= 8
			
			temp0 = temp1
			temp0 += temp2
			temp0 += color7
			SetPaletteEntry(0, 6, temp0)
			DrawSpriteScreenXY(object.frame, object.ixpos, object.iypos)
		end if	
	end if	
end event

event ObjectStartup

	LoadSpriteSheet("DDLCMenu/TextBox.gif")

	SpriteFrame(0, 0, 0, 0, 0, 0) // 0 Blank
	SpriteFrame(0, 0, 0, 0, 0, 0) // 1 Blank

	temp0 = 0
	temp1 = 77
	while temp0 < 26
		SpriteFrame(0, -3, 7, 7, temp1, 171)	//2 - 27 Uppercase
		temp0++
		temp1 += 8
	loop

	temp0 = 0
	temp1 = 77
	while temp0 < 26
		SpriteFrame(0, -3, 7, 8, temp1, 179) //28 - 53 Lowercase
		temp0++
		temp1 += 8
	loop	
	
	temp0 = 0
	temp1 = 188
	while temp0 < 10
		SpriteFrame(0, -3, 5, 7, 85, temp1) //54 - 63 Numbers
		temp0++
		temp1 += 8
	loop	
	
	temp0 = 91
	SpriteFrame(0, -3, 3, 7, temp0, 188)	//64 (
	temp0 += 4
	SpriteFrame(0, -3, 3, 7, temp0, 188)	//65 )
	temp0 += 4
	SpriteFrame(0, -3, 3, 7, temp0, 188)	//66 "
	temp0 += 4
	SpriteFrame(0, -3, 1, 7, temp0, 188)	//67 .
	temp0 += 2
	SpriteFrame(0, -3, 4, 7, temp0, 188)	//68 -
	temp0 += 5
	SpriteFrame(0, -3, 2, 8, temp0, 188)	//69 ,
	temp0 += 3
	SpriteFrame(0, -3, 1, 7, temp0, 188)	//70 !	
	temp0 += 2
	SpriteFrame(0, -3, 0, 0, 0, 0)		//71 Space
	SpriteFrame(0, -3, 5, 7, temp0, 188)	//72 ?
	temp0 += 6
	SpriteFrame(0, -3, 1, 7, temp0, 188)	//73 '
	temp0 += 2
	SpriteFrame(0, -3, 1, 7, temp0, 188)	//74 :
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
